using DG.Tweening;
using UnityEngine;

public class CheckpointInstant : MonoBehaviour
{
	public GameObject spawnPointObject;

	public Vector3 spawnPosition;

	public Quaternion spawnRotation;

	public int checkPointNumber = -1;

	public GameObject checkpointParticles;

	public GameObject checkpointBoby;

	public ParticleSystem particlesRayOn;

	public ParticleSystem particlesSparklesOn;

	public ParticleSystem particlesSparklesOff;

	public GameObject bodyBase;

	public GameObject bodyRing;

	public Material materialOn;

	public Material materialOff;

	private bool isActive;

	private bool isActivated;

	private Sequence seqRing1;

	private Sequence seqRing2;

	private Vector3 ringInitScale;

	private Color ringInitColor;

	private Vector3 ringStartPos = Vector3.zero;

	private Vector3 ringEndPos = Vector3.up * 2.5f;

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		EventManager.WorldResetEvent += OnWorldReset;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		EventManager.WorldResetEvent -= OnWorldReset;
	}

	private void Update()
	{
		if (isActive && !isActivated)
		{
			particlesSparklesOff.gameObject.transform.Rotate(Vector3.forward * Time.deltaTime * 50f);
		}
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			Initialize();
		}
	}

	private void OnWorldReset()
	{
		Deactivate();
	}

	private void Initialize()
	{
		spawnPosition = spawnPointObject.transform.position;
		spawnRotation = spawnPointObject.transform.rotation;
		Object.Destroy(spawnPointObject);
		isActive = true;
		ringInitColor = bodyRing.GetComponent<Renderer>().material.GetColor("_TintColor");
		ringInitScale = bodyRing.transform.localScale;
		Deactivate();
	}

	private void OnTriggerEnter(Collider other)
	{
		if (isActive && other.gameObject == BallBehaviour.GetInstance().gameObject && !isActivated)
		{
			Activate();
		}
	}

	private void Activate()
	{
		isActivated = true;
		SpawnPoint.SetPosition(spawnPosition);
		SpawnPoint.SetRotation(spawnRotation);
		Audio.PlaySoundCheckpoint();
		SetActivated();
		Game.FireCheckpointActivatedEvent(checkPointNumber);
	}

	private void Deactivate()
	{
		isActivated = false;
		SetNotActivated();
	}

	private void SetActivated()
	{
		GetComponent<CapsuleCollider>().enabled = false;
		particlesSparklesOff.Stop();
		particlesSparklesOff.gameObject.SetActive(false);
		particlesRayOn.gameObject.SetActive(true);
		particlesRayOn.Play();
		particlesSparklesOn.gameObject.SetActive(true);
		particlesSparklesOn.Play();
		bodyBase.GetComponent<Renderer>().sharedMaterial = materialOn;
		StopBodyRingAnimation();
	}

	private void SetNotActivated()
	{
		GetComponent<CapsuleCollider>().enabled = true;
		particlesRayOn.Stop();
		particlesRayOn.gameObject.SetActive(false);
		particlesSparklesOn.Stop();
		particlesSparklesOn.gameObject.SetActive(false);
		particlesSparklesOff.gameObject.SetActive(true);
		particlesSparklesOff.Play();
		bodyBase.GetComponent<Renderer>().sharedMaterial = materialOff;
		StartBodyRingAnimation();
	}

	private void StartBodyRingAnimation()
	{
		if (seqRing1 != null)
		{
			seqRing1.Kill();
			seqRing2.Kill();
			seqRing1 = null;
			seqRing2 = null;
		}
		bodyRing.SetActive(true);
		bodyRing.transform.localPosition = ringStartPos;
		UpdateRingAlpha(1f);
		seqRing1 = DOTween.Sequence();
		seqRing2 = DOTween.Sequence();
		seqRing1.Append(bodyRing.transform.DOLocalMove(ringEndPos, 0.6f).SetEase(Ease.Linear).OnUpdate(OnUpdateRing));
		seqRing2.Append(DOTween.To(UpdateRingAlpha, ringInitColor.a, 0f, 0.6f).SetEase(Ease.OutSine));
		seqRing1.AppendInterval(0.4f);
		seqRing2.AppendInterval(0.4f);
		seqRing1.SetLoops(-1);
		seqRing2.SetLoops(-1);
		seqRing1.Play();
		seqRing2.Play();
	}

	private void StopBodyRingAnimation()
	{
		if (seqRing1 != null)
		{
			seqRing1.Kill();
			seqRing2.Kill();
			seqRing1 = null;
			seqRing2 = null;
		}
		bodyRing.SetActive(false);
	}

	private void OnUpdateRing()
	{
		float num = Mathf.InverseLerp(ringEndPos.y * 5f, ringStartPos.y, bodyRing.transform.localPosition.y);
		bodyRing.transform.localScale = new Vector3(ringInitScale.x * num, ringInitScale.y * num, ringInitScale.z);
	}

	private void UpdateRingAlpha(float alpha)
	{
		bodyRing.GetComponent<Renderer>().material.SetColor("_TintColor", Utils.SetAlpha(ringInitColor, alpha));
	}
}
